﻿package mortal.component.gLinkText
{
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.task.data.*;
    import mortal.mvc.core.*;

    public class GLinkText extends GSprite
    {
        private var _myData:GLinkTextData;
        private var _btnFlyBoot:FlyBoot;
        private var _txt:SecTimerView;
        private var _isShowNum:Boolean = true;
        private var _isShowFlyBoot:Boolean = true;
        private var _widthConst:int = 180;
        private var _btnFix:GLoadedButton;
        private var _txtTips:GTextFiled;
        private var _callback:Function;

        public function GLinkText()
        {
            return;
        }// end function

        override public function get height() : Number
        {
            if (this._txtTips != null && this._txtTips.parent != null)
            {
                return this._txtTips.y + 18;
            }
            if (this._btnFix != null && this._btnFix.parent != null)
            {
                return this._btnFix.y + this._btnFix.height;
            }
            var _loc_1:* = this._txt.textHeight + 2;
            if (this._btnFlyBoot != null && this._btnFlyBoot.parent != null)
            {
                return Math.max(this._btnFlyBoot.y + this._btnFlyBoot.height, _loc_1);
            }
            return this._txt.textHeight + 2;
        }// end function

        public function get myData() : GLinkTextData
        {
            return this._myData;
        }// end function

        public function get widthConst() : int
        {
            return this._widthConst;
        }// end function

        public function set widthConst(param1:int) : void
        {
            this._widthConst = param1;
            this._txt.width = this._widthConst;
            return;
        }// end function

        public function get isShowFlyBoot() : Boolean
        {
            return this._isShowFlyBoot;
        }// end function

        public function set isShowFlyBoot(param1:Boolean) : void
        {
            this._isShowFlyBoot = param1;
            return;
        }// end function

        public function get isShowNum() : Boolean
        {
            return this._isShowNum;
        }// end function

        public function set isShowNum(param1:Boolean) : void
        {
            this._isShowNum = param1;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            var _loc_1:* = GlobalStyle.textFormatBai;
            _loc_1.leading = 2;
            this._txt = UIFactory.secTimeView("", 0, 0, 220, 60, this, _loc_1);
            this._txt.filters = [FilterConst.glowFilter];
            this._txt.wordWrap = true;
            this._txt.multiline = true;
            this._txt.mouseEnabled = true;
            this._txt.addEventListener(TextEvent.LINK, this.linkHandler);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._txt.dispose(param1);
            this._txt.removeEventListener(TextEvent.LINK, this.linkHandler);
            this._txt.timeOutHandler = null;
            this._txt = null;
            if (this._btnFlyBoot != null)
            {
                this._btnFlyBoot.dispose(param1);
                this._btnFlyBoot = null;
            }
            this._myData = null;
            this._isShowNum = true;
            this._isShowFlyBoot = true;
            this._callback = null;
            this._widthConst = 180;
            if (this._txtTips != null)
            {
                this._txtTips.dispose(param1);
                this._txtTips = null;
            }
            return;
        }// end function

        public function setLinkText(param1:GLinkTextData, param2:String = "", param3:String = "") : void
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:TextLineMetrics = null;
            this._myData = param1;
            var _loc_4:String = "";
            if (this._myData.htmlText != null)
            {
                if (param1.isLieMoTask)
                {
                    _loc_4 = param3 + this._myData.htmlText;
                    _loc_4 = _loc_4.replace("{0}", param1.curNum.toString());
                }
                else if (param1.isShowNum && this._isShowNum)
                {
                    _loc_4 = param3 + this._myData.htmlText + Language.getStringByParam(20163, param1.curNum, param1.totalNum);
                }
                else
                {
                    _loc_4 = param3 + this._myData.htmlText;
                }
            }
            if (!this._myData.isLevelEnouth && Cache.instance.role.roleInfo.level < this._myData.needLevel)
            {
                _loc_4 = _loc_4 + Language.getStringByParam(20369, this._myData.needLevel);
                this._myData.needBoot = false;
            }
            this._txt.height = 40;
            this._txt.width = this.widthConst;
            var _loc_5:int = 20;
            this._txt.htmlText = _loc_4;
            if (this._myData.endSecond != GLinkTextData.NoTimer)
            {
                _loc_6 = this._myData.endSecond - ClockManager.instance.nowDate.time / 1000;
                this._txt.setParse(_loc_4);
                this._txt.timeOutHandler = this.timeOutHandler;
                this._txt.setLeftTime(_loc_6);
            }
            else if (this._txt.running)
            {
                this._txt.stop();
                this._txt.htmlText = _loc_4;
            }
            this._txt.width = this._txt.textWidth + 8;
            this._txt.height = this._txt.textHeight + this._txt.numLines * 2;
            _loc_5 = this._txt.height;
            if (this._myData.needBoot && this._isShowFlyBoot)
            {
                if (this._btnFlyBoot == null)
                {
                    this._btnFlyBoot = UICompomentPool.getUICompoment(FlyBoot) as FlyBoot;
                    this._btnFlyBoot.x = 16;
                    this._btnFlyBoot.y = 18;
                    this._btnFlyBoot.configEventListener(MouseEvent.CLICK, this.flyBootHandler);
                }
                this._btnFlyBoot.gLinkTextData = this._myData;
                if (this._btnFlyBoot.parent == null)
                {
                    this.addChild(this._btnFlyBoot);
                }
                _loc_7 = this._txt.getLineMetrics((this._txt.numLines - 1)).width;
                if (_loc_7 <= _loc_5 - 20 || this._widthConst - _loc_7 >= 20)
                {
                    this._btnFlyBoot.x = _loc_7 + 2;
                    this._btnFlyBoot.y = _loc_5 - 22;
                    if (param1.isLieMoTask)
                    {
                        this._btnFlyBoot.y = _loc_5 - 27;
                    }
                }
                else
                {
                    this._btnFlyBoot.x = 0;
                    this._btnFlyBoot.y = _loc_5 - 2;
                }
            }
            else
            {
                DisplayUtil.removeMe(this._btnFlyBoot);
            }
            if (this._myData.isNewCollectType())
            {
                if (this._btnFix == null)
                {
                    this._btnFix = UIFactory.gLoadedButton(ImagesConst.MiningFix_upSkin, 0, 0, 16, 16, this);
                    this._btnFix.configEventListener(MouseEvent.CLICK, this.clickFixHandler);
                }
                if (this._btnFix.parent == null)
                {
                    this.addChild(this._btnFix);
                }
                _loc_7 = this._txt.getLineMetrics((this._txt.numLines - 1)).width;
                if (_loc_7 <= _loc_5 - 20 || this._widthConst - this._txt.textWidth >= 20)
                {
                    this._btnFix.x = _loc_7 + 2;
                    this._btnFix.y = _loc_5 - 20;
                }
                else
                {
                    this._btnFix.x = 0;
                    this._btnFix.y = _loc_5;
                }
                _loc_8 = this._txt.getLineMetrics(0);
                if (this._btnFlyBoot != null)
                {
                    this._btnFlyBoot.x = _loc_8.width + 6;
                    this._btnFlyBoot.y = 0;
                }
            }
            else if (this._btnFix != null)
            {
                this._btnFix.dispose(true);
                this._btnFix = null;
            }
            if (this._myData != null && this._myData.tips != null)
            {
                if (this._txtTips == null)
                {
                    this._txtTips = UIFactory.gTextField("", 0, 0, 220, 20, this);
                    this._txtTips.configEventListener(TextEvent.LINK, this.linkHandler);
                }
                if (this._txtTips.parent == null)
                {
                    this.addChild(this._txtTips);
                }
                this._txtTips.htmlText = this._myData.tips;
                this._txtTips.y = _loc_5 - 2;
            }
            else if (this._txtTips != null)
            {
                DisplayUtil.removeMe(this._txtTips);
                this._txtTips.dispose(true);
                this._txtTips = null;
            }
            return;
        }// end function

        private function clickFixHandler(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MiningShowModule, true));
            return;
        }// end function

        private function timeOutHandler() : void
        {
            return;
        }// end function

        override public function get width() : Number
        {
            if (this._txt != null && this._txt.numLines == 1)
            {
                if (this._btnFlyBoot != null && this._btnFlyBoot.y < 20)
                {
                    return this._btnFlyBoot.x + this._btnFlyBoot.width;
                }
                return this._txt.textWidth + 8;
            }
            return this._widthConst;
        }// end function

        public function get flyBoot() : DisplayObject
        {
            return this._btnFlyBoot;
        }// end function

        public function set clickHandler(param1:Function) : void
        {
            this._callback = param1;
            return;
        }// end function

        private function linkHandler(event:TextEvent) : void
        {
            var _loc_2:* = event.text;
            this.linkImpl(_loc_2);
            return;
        }// end function

        public function linkImpl(param1:String) : void
        {
            TaskUtil.linkTaskImpl(this._myData, param1);
            if (this._callback != null)
            {
                this._callback.apply();
                this._callback = null;
            }
            return;
        }// end function

        private function flyBootHandler(event:MouseEvent) : void
        {
            if (this._callback != null)
            {
                this._callback.apply();
                this._callback = null;
            }
            return;
        }// end function

    }
}
